import dataApi from '@/api/data.js'
import { BillApi } from '@/api/Bill.api.js'
import { Common } from '@/utils/common.js'
import moment from 'moment'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: '',

  data () {
    return {
      isShowTableHuizongbaobiaoModal: false,
      companyName: null,
      companyId: null,
      year: null,
      month: null,
      showYear: null,
      showMonth: null,
      remark: null,
      tableData: [],
      newTableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      payDeadLineDay: '',
      banks: [],
      byActually: 'true',
      arrListDown: {
        byActuallyOps: [
          { label: '按实发工资显示', value: 'true' },
          { label: '按应发工资显示', value: 'false' }
        ]
      },
      billConfirmDay: ''
    }
  },

  watch: {
  },

  mounted () {
    // this.companyName = this.$route.query.companyName
    this.getBillCollectExport()
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      this.newTableData = []
      var params = {
        customerId: this.companyId,
        year: this.year,
        month: this.month,
        byActually: this.byActually
      }
      console.log('汇总报表params: ', params)
      let res = await BillApi.allFeeTotal(params)
      console.log('汇总报表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.remark = res.data.message
        var result = res.data.data
        result.map(item => {
          item.billFees.map(c_item => {
            var str = c_item.code + 'Amt'
            item[str] = c_item.amt
          })
          console.log('item: ', item)
        })
        this.newTableData = result
      } else {
        this.newTableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    // 获取银行账户信息
    async customerPayInfo () {
      dataApi.customerPayInfo({ id: this.companyId }).then(res => {
        console.log('获取银行账户信息res: ', res)
        if (res.status === 200 && res.data.success) {
          this.payDeadLineDay = res.data.data.payDeadLineDay
          // this.payDeadLineDay = moment(res.data.data.payDeadLineDay).format('MM-DD')
          this.showYear = res.data.data.payDeadLineDayType == 1 && this.month == 12 ? this.year + 1 : this.year
          this.showMonth = res.data.data.payDeadLineDayType == 0 ? this.month : (this.month == 12 ? 1 : this.month + 1)
          this.banks = res.data.data.banks
        }
      })
    },

    // 获取账单确认时间
    async getBillCollectExport () {
      BillApi.getBillCollectExport().then(res => {
        console.log('获取账单确认时间res: ', res)
        if (res.status === 200 && res.data.success) {
          this.billConfirmDay = res.data.data
        }
      })
    },

    ok () {
    },

    cancel () {
    },

    handleExportData () {
    },

    parsingItems (row, item, oneTitle, twoTitle1, twoTitle2, type) {
      var sumTitle = oneTitle + twoTitle1 + type
      console.log('sumTitle: ', sumTitle)
      row[sumTitle] = item[twoTitle1][type]
    },

    // 汇总报表导出
    async exportALLTotal () {
      var params = {
        companyID: this.companyId,
        companyName: this.companyName,
        year: this.year,
        month: this.month,
        insures: this.newTableData,
        remark: this.remark
      }
      console.log('汇总报表导出params: ', params)
      let res = await BillApi.exportALLTotal(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 汇总报表导出PDF
    async exportALLTotalPDF () {
      var params = {
        companyID: this.companyId,
        companyName: this.companyName,
        year: this.year,
        month: this.month,
        insures: this.newTableData,
        remark: this.remark
      }
      console.log('汇总报表导出params: ', params)
      let res = await BillApi.exportALLTotalPDF(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 汇总报表导出
    async exportALLTotalCaiZhi () {
      var params = {
        companyID: this.companyId,
        companyName: this.companyName,
        year: this.year,
        month: this.month,
        insures: this.newTableData,
        remark: this.remark
      }
      console.log('汇总报表导出params: ', params)
      let res = await BillApi.exportALLTotalCaiZhi(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 汇总报表导出PDF
    async exportALLTotalPDFCaiZhi () {
      var params = {
        companyID: this.companyId,
        companyName: this.companyName,
        year: this.year,
        month: this.month,
        insures: this.newTableData,
        remark: this.remark
      }
      console.log('汇总报表导出params: ', params)
      let res = await BillApi.exportALLTotalPDFCaiZhi(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 显示方式改变
    handleByActuallyChange (byActually) {
      console.log('byActually: ', byActually)
      this.query()
    }
  }
}
